<!DOCTYPE html>
<html
  lang="zh"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
>
  <head>
    <th:block th:include="include :: header('修改政治生日')" />
    <th:block th:include="include :: summernote-css" />
  </head>
  <body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
      <div class="row">
        <div class="col-sm-12">
          <div class="ibox float-e-margins">
            <div class="ibox-title">
              <h5>修改政治生日</h5>
            </div>
            <div class="ibox-content">
              <form
                class="form-horizontal m"
                id="form-political-birthday-edit"
                th:object="${djPoliticalBirthday}"
                enctype="multipart/form-data"
              >
                <input name="id" th:field="*{id}" type="hidden" />
                <div class="form-group">
                  <label class="col-sm-3 control-label">党员：</label>
                  <div class="col-sm-8">
                    <input
                      name="partyMemberId"
                      th:field="*{partyMemberId}"
                      id="partyMemberId"
                      type="hidden"
                    />
                    <input
                      name="participants"
                      th:field="*{participants}"
                      id="participants"
                      type="hidden"
                    />
                    <input
                      name="participantNames"
                      th:field="*{participantNames}"
                      id="participantNames"
                      type="hidden"
                    />
                    <div class="input-group">
                      <input
                        class="form-control"
                        id="memberName"
                        readonly
                        required
                      />
                      <div class="input-group-btn">
                        <button
                          type="button"
                          class="btn btn-primary"
                          onclick="selectPartyMember()"
                        >
                          选择党员
                        </button>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label">政治生日日期：</label>
                  <div class="col-sm-8">
                    <div class="input-group date">
                      <input
                        name="birthdayDate"
                        th:field="*{birthdayDate}"
                        class="form-control"
                        type="text"
                        id="birthdayDate"
                        readonly
                        required
                      />
                      <span class="input-group-addon"
                        ><i class="fa fa-calendar"></i
                      ></span>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label">生日祝福内容：</label>
                  <div class="col-sm-8">
                    <textarea
                      name="wishContent"
                      id="wishContent"
                      th:field="*{wishContent}"
                      class="form-control"
                    ></textarea>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label">通知方式：</label>
                  <div class="col-sm-8">
                    <label class="radio-box">
                      <input
                        type="radio"
                        name="noticeMethods"
                        value="短信"
                        th:checked="*{noticeMethods == '短信'}"
                      />
                      活动前发送短信提醒
                    </label>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label">活动资料上传：</label>
                  <div class="col-sm-8">
                    <input name="videoFile" class="form-control" type="file" />
                    <span class="help-block m-b-none"
                      ><i class="fa fa-info-circle"></i>
                      上传活动相关资料，包括讲话稿、学习材料、照片等</span
                    >
                    <div th:if="*{videoPath != null and videoPath != ''}">
                      已上传文件:
                      <a th:href="@{*{videoPath}}" target="_blank">查看文件</a>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label">状态：</label>
                  <div class="col-sm-8">
                    <select
                      name="status"
                      th:field="*{status}"
                      class="form-control"
                    >
                      <option value="0">未开始</option>
                      <option value="1">进行中</option>
                      <option value="2">已结束</option>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label">备注：</label>
                  <div class="col-sm-8">
                    <textarea
                      name="remark"
                      th:field="*{remark}"
                      class="form-control"
                      rows="3"
                    ></textarea>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>

    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />

    <script type="text/javascript">
      var prefix = ctx + "dj/political/birthday";

      // 初始化日期选择器
      $("#birthdayDate").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true,
      });

      // 初始化富文本编辑器
      $("#wishContent").summernote({
        height: 200,
        lang: "zh-CN",
        toolbar: [
          ["style", ["style"]],
          ["font", ["bold", "underline", "clear"]],
          ["fontname", ["fontname"]],
          ["color", ["color"]],
          ["para", ["ul", "ol", "paragraph"]],
          ["table", ["table"]],
          ["insert", ["link", "picture"]],
          ["view", ["fullscreen", "codeview", "help"]],
        ],
      });

      // 如果有初始内容，设置富文本编辑器的内容
      var initialContent = $("#wishContent").val();
      if (initialContent) {
        $("#wishContent").summernote("code", initialContent);
      }

      $("#form-political-birthday-edit").validate({
        rules: {
          partyMemberId: {
            required: true,
          },
          memberName: {
            required: true,
            maxlength: 100,
          },
          birthdayDate: {
            required: true,
          },
        },
        messages: {
          partyMemberId: {
            required: "请选择党员",
          },
          memberName: {
            required: "请选择党员",
            maxlength: "党员姓名不能超过100个字符",
          },
          birthdayDate: {
            required: "请选择政治生日日期",
          },
        },
        submitHandler: function (form) {
          // 更新textarea的值为富文本编辑器的内容
          $("#wishContent").val($("#wishContent").summernote("code"));

          // 创建FormData对象
          var formData = new FormData(form);

          // 提交表单
          $.ajax({
            url: prefix + "/edit",
            type: "POST",
            data: formData,
            contentType: false,
            processData: false,
            beforeSend: function () {
              $.modal.loading("正在处理中，请稍候...");
              $.modal.disable();
            },
            success: function (result) {
              // 使用框架标准的成功回调处理
              $.operate.successCallback(result);
            },
            error: function () {
              $.modal.closeLoading();
              $.modal.enable();
              $.modal.msgError("系统错误，请重试");
            },
          });
        },
      });

      // 全局submitHandler函数，用于iframe弹窗调用
      function submitHandler() {
        if ($.validate.form()) {
          $("#form-political-birthday-edit").submit();
        }
      }

      // 选择党员
      function selectPartyMember() {
        layer.open({
          type: 2,
          title: "选择党员",
          area: ["800px", "500px"],
          content: ctx + "system/partymember/member/select",
          btn: ["确定", "关闭"],
          yes: function (index, layero) {
            var iframeWin = layero.find("iframe")[0];
            var data = iframeWin.contentWindow.getSelected();
            if (data && data.length > 0) {
              if (data.length === 1) {
                // 单选模式：只选择了一个党员
                $("#partyMemberId").val(data[0].id);
                $("#participants").val(""); // 清空多选数据
                $("#participantNames").val(""); // 清空多选名称
                $("#memberName").val(data[0].name);
              } else {
                // 多选模式：选择了多个党员
                var memberIds = [];
                var memberNames = [];

                for (var i = 0; i < data.length; i++) {
                  memberIds.push(data[i].id);
                  memberNames.push(data[i].name);
                }

                // 设置第一个党员为主要党员（兼容原有字段）
                $("#partyMemberId").val(data[0].id);

                // 设置多选党员数据
                $("#participants").val(memberIds.join(","));
                $("#participantNames").val(memberNames.join(","));

                // 显示所有选中的党员姓名
                $("#memberName").val(memberNames.join(", "));
              }

              // 自动调整textarea高度
              adjustTextareaHeight();
            }
            layer.close(index);
          },
        });
      }

      // 自动调整textarea高度
      function adjustTextareaHeight() {
        var textarea = document.getElementById("memberName");
        textarea.style.height = "auto";
        textarea.style.height = Math.max(34, textarea.scrollHeight) + "px";
      }

      // 页面加载完成后调整textarea高度
      $(document).ready(function () {
        setTimeout(function () {
          // 初始化显示党员名称
          initMemberNameDisplay();
          adjustTextareaHeight();
        }, 100);
      });

      // 初始化党员名称显示
      function initMemberNameDisplay() {
        var participantNames = $("#participantNames").val();

        if (participantNames && participantNames.trim() !== "") {
          // 如果有多选党员名称，显示多选名称
          $("#memberName").val(participantNames);
        } else {
          // 如果没有多选数据，使用后端传递的memberName
          var memberName = /*[[${djPoliticalBirthday.memberName}]]*/ "";
          if (memberName) {
            $("#memberName").val(memberName);
          }
        }
      }

      function closeItem() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
      }
    </script>
  </body>
</html>
